﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using DLL;

namespace BLL
{
   public class ThietBiBLL
    {
       DataServer DB = new DataServer();
       public DataTable Select_ALL()
       {
           string sql = string.Format("sp_ThietBi_GetByAll");
           DataTable bang = DB.LayDuLieuProc(sql);
           return bang;
       }
       public bool Them(ThietBiDLL tb)
       {
           string sql = string.Format("sp_ThietBi_Insert");
           string MaTB = DLL.CommonFunc.GetPrimaryKey("ThietBi");
           SqlParameter paramMTB = new SqlParameter("@MaTB",MaTB);
           SqlParameter paramTTB = new SqlParameter("@TenTB", tb.TenTB);
           SqlParameter paramNCC = new SqlParameter("@MaNCC", tb.MaNCC);
           SqlParameter paramBH = new SqlParameter("@BaoHanh", tb.BaoHanh);
           SqlParameter paramSL = new SqlParameter("@SoLuong", tb.SoLuong);
           SqlParameter paramMT = new SqlParameter("@MoTa", tb.MoTa);
           SqlParameter paramGT = new SqlParameter("@GiaTien", tb.GiaTien);
           SqlParameter paramNID = new SqlParameter("@NhomID", tb.NhomID);
           SqlParameter paramTT = new SqlParameter("@TonTai", tb.TonTai);
           try
           {
               DB.CapNhatProc(sql, paramMTB, paramTTB, paramNCC, paramBH, paramSL, paramMT, paramGT, paramNID, paramTT);
           }
           catch
           {
               return false;
           }
           DLL.CommonFunc.UpdatePrimaryKey("ThietBi");
           return true;
       }
       public bool Sua(ThietBiDLL tb)
       {
           string sql = string.Format("sp_ThietBi_Update");
           SqlParameter paramMTB = new SqlParameter("@MaTB", tb.MaTB);
           SqlParameter paramTTB = new SqlParameter("@TenTB", tb.TenTB);
           SqlParameter paramNCC = new SqlParameter("@MaNCC", tb.MaNCC);
           SqlParameter paramBH = new SqlParameter("@BaoHanh", tb.BaoHanh);
           SqlParameter paramSL = new SqlParameter("@SoLuong", tb.SoLuong);
           SqlParameter paramMT = new SqlParameter("@MoTa", tb.MoTa);
           SqlParameter paramGT = new SqlParameter("@GiaTien", tb.GiaTien);
           SqlParameter paramNID = new SqlParameter("@NhomID", tb.NhomID);
           SqlParameter paramTT = new SqlParameter("@TonTai", tb.TonTai);
           try
           {
               DB.CapNhatProc(sql, paramMTB, paramTTB, paramNCC, paramBH, paramSL, paramMT, paramGT, paramNID, paramTT);
           }
           catch
           {
               return false;
           }
           return true;
       }
       public bool Xoa(string MaTB)
       {
           string sql = string.Format("sp_ThietBi_Delete");
           SqlParameter paramMTB = new SqlParameter("@MaTB",MaTB);
           try
           {
               DB.CapNhatProc(sql, paramMTB);
           }
           catch
           {
               return false;
           }
           return true;
       }
       public DataTable SelectALL_NhomID()
       {
           string sql = string.Format("sp_NhomThietBi_GetByAll");
           DataTable bang = DB.LayDuLieuProc(sql);
           return bang;
       }
       public DataTable SelectALL_NhaCC()
       {
           string sql = "select *from NhaCC";
           DataTable bang = DB.LayDulieu(sql);
           return bang;
       }
       public DataTable Search(ThietBiDLL tb)
       {
             string sql = string.Format("sp_ThietBi_Search");
           SqlParameter paramMTB = new SqlParameter("@MaTB", tb.MaTB);
           SqlParameter paramTTB = new SqlParameter("@TenTB", tb.TenTB);
           SqlParameter paramMT = new SqlParameter("@MoTa", tb.MoTa);
           DataTable bang = DB.LayDuLieuProc(sql, paramMTB, paramTTB, paramMT);
           return bang;
       }
       public bool Kiemtra(string TenTB)
       {
           string sql = "select TenTB from ThietBi where  TenTB=@TenTB";
           SqlParameter paramMNTB = new SqlParameter("@TenTB",TenTB);
           DataTable bang = DB.LayDulieu(sql, paramMNTB);
           if (bang.Rows.Count > 0)
           {
               return true;
           }
           return false;
       }
       public DataTable LayTheoMaNTB(string NhomID)
       {
           string sql = @"SELECT ThietBi.MaTB, ThietBi.TenTB, NhaCC.TenNCC, ThietBi.BaoHanh, ThietBi.SoLuong, ThietBi.MoTa, ThietBi.GiaTien, NhomThietBi.TenNhomID
                         FROM NhaCC INNER JOIN ThietBi ON NhaCC.MaNCC = ThietBi.MaNCC INNER JOIN NhomThietBi ON ThietBi.NhomID = NhomThietBi.NhomID
                          where TonTai=1 and ThietBi.NhomID =@NhomID";
           SqlParameter paramNhomID = new SqlParameter("@NhomID", NhomID);
           DataTable bang = DB.LayDulieu(sql,paramNhomID);
           return bang;
       }
       public DataTable LayTheoNCC(string NCC)
       {
           string sql = @"SELECT ThietBi.MaTB, ThietBi.TenTB, NhaCC.TenNCC, ThietBi.BaoHanh, ThietBi.SoLuong, ThietBi.MoTa, ThietBi.GiaTien, NhomThietBi.TenNhomID
                         FROM NhaCC INNER JOIN ThietBi ON NhaCC.MaNCC = ThietBi.MaNCC INNER JOIN NhomThietBi ON ThietBi.NhomID = NhomThietBi.NhomID
                          where TonTai=1 and ThietBi.MaNCC =@MaNCC";
           SqlParameter paramNhomID = new SqlParameter("@MaNCC", NCC);
           DataTable bang = DB.LayDulieu(sql, paramNhomID);
           return bang;
       }
    }
}
